package com.kaizzi.data.jdl.helper;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.kaizzi.data.common.utils.EasyExcelUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.jdl.model.*;
import com.kaizzi.data.jdl.service.LogService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author Kzen
 * @Description 读取excel
 * @date 2020/9/10
 */
@Component
public class ExcelHelper {

    @Resource
    private LogService logService;

    /**
     * @return 表格内所有数据
     */
    public ExcelModel readExcel() {
        ExcelModel model = new ExcelModel();
        try {
            InputStream inputStream = getInputStream();
            List<CommentsModel> comments = EasyExcelUtil.readExcelWithModel(inputStream, CommentsModel.class, ExcelTypeEnum.XLSX,1);
            model.setComments(comments);
            inputStream.close();

            InputStream inputStream1 = getInputStream();
            List<Page7Model> page7s = EasyExcelUtil.readExcelWithModel(inputStream1, Page7Model.class, ExcelTypeEnum.XLSX, 2);
            model.setPage7s(page7s);
            inputStream1.close();

            InputStream inputStream2 = getInputStream();
            List<Page8Model> page8s = EasyExcelUtil.readExcelWithModel(inputStream2, Page8Model.class, ExcelTypeEnum.XLSX, 3);
            model.setPage8s(page8s);
            inputStream2.close();

            InputStream inputStream4 = getInputStream();
            List<Page12Model> page12s = EasyExcelUtil.readExcelWithModel(inputStream4, Page12Model.class, ExcelTypeEnum.XLSX, 4);
            model.setPage12s(page12s);
            inputStream2.close();

            InputStream inputStream5 = getInputStream();
            List<Page15Model> page15s = EasyExcelUtil.readExcelWithModel(inputStream5, Page15Model.class, ExcelTypeEnum.XLSX, 5);
            model.setPage15s(page15s);
            inputStream5.close();

            InputStream inputStream6 = getInputStream();
            List<Page17Model> page17s = EasyExcelUtil.readExcelWithModel(inputStream6, Page17Model.class, ExcelTypeEnum.XLSX, 6);
            model.setPage17s(page17s);
            inputStream6.close();

            InputStream inputStream7 = getInputStream();
            List<Page20Model> page20s = EasyExcelUtil.readExcelWithModel(inputStream7, Page20Model.class, ExcelTypeEnum.XLSX, 7);
            model.setPage20s(page20s);
            inputStream7.close();

            InputStream inputStream8 = getInputStream();
            List<Page25Model> page25s = EasyExcelUtil.readExcelWithModel(inputStream8, Page25Model.class, ExcelTypeEnum.XLSX, 8);
            model.setPage25s(page25s);
            inputStream8.close();

            InputStream inputStream9 = getInputStream();
            List<Page32Model> page32s = EasyExcelUtil.readExcelWithModel(inputStream9, Page32Model.class, ExcelTypeEnum.XLSX, 9);
            model.setPage32s(page32s);
            inputStream9.close();

            InputStream inputStream10 = getInputStream();
            List<Page35Model> page35s = EasyExcelUtil.readExcelWithModel(inputStream10, Page35Model.class, ExcelTypeEnum.XLSX, 10);
            model.setPage35s(page35s);
            inputStream10.close();

            InputStream inputStream11 = getInputStream();
            List<Page39Model> page39s = EasyExcelUtil.readExcelWithModel(inputStream11, Page39Model.class, ExcelTypeEnum.XLSX, 11);
            model.setPage39s(page39s);
            inputStream11.close();

//            InputStream inputStream12 = getInputStream();
//            List<Page45Model> page45s = EasyExcelUtil.readExcelWithModel(inputStream12, Page45Model.class, ExcelTypeEnum.XLSX, 12);
//            model.setPage45s(page45s);
//            inputStream11.close();

        } catch (IOException e) {
            e.printStackTrace();
            //表格数据异常
            //记录异常日志
            logService.setDataLogStatus("excel-error");
            return null;
        }
        return model;
    }


    private static InputStream getInputStream() throws FileNotFoundException {
        return new FileInputStream("jdlExcel.xlsx");
    }


}
